/*
Miranda IM for Windows Search

Copyright (C) 2012-2014 Nikolay Raspopov

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#pragma once

#include "resource.h"
#include "MirandaSearch_i.h"

// Command pipe name
#define COMMAND_CHANNEL	_T("\\\\.\\pipe\\MirandaSearch")

// Protocol name
#define PROTOCOL		_T("miranda://")

// "miranda://" protocol item type (ProgID)
#define TYPE			_T("MirandaSearch.MirandaProtocol")

// Item folder name
#define TITLE			_T(MIRANDANAME)

// Name of data folder (relative to Plugins)
#define DATA_DIR		_T("MirandaSearch Data")

// Name of MSI registration file (relative to data folder)
#define MSI_FILE		_T("mspr.msi")

// Security zone for IE
#define ZONE			_T("file:///C:\\")

// Pipe buffers sizes
#define BUFSIZE			( 1024 * 1024 )

// Plugin ID https://addons.miranda-im.org/details.php?action=viewfile&id=4498
#define FILE_ID			( 4498 )

// Plugin GUID : {ED108873-2D16-4D14-9D46-8F09923FFC5D}
#define MIID_SEARCH		{ 0xed108873, 0x2d16, 0x4d14, { 0x9d, 0x46, 0x8f, 0x9, 0x92, 0x3f, 0xfc, 0x5d } }

enum URLLevel
{
	URL_LEVEL_BAD,			// Unknown URL
	URL_LEVEL_ROOT,			// "miranda:///"
	URL_LEVEL_CONTACTS,		// "miranda://{SID}/"
	URL_LEVEL_EVENTS,		// "miranda://{SID}/contact/"
	URL_LEVEL_MESSAGE		// "miranda://{SID}/contact/message" where "message" is a number in range 1..n or "last"
};

// CMirandaSearchModule

class CMirandaSearchModule : public CAtlDllModuleT< CMirandaSearchModule >
{
public :
	CMirandaSearchModule();
	virtual ~CMirandaSearchModule();

	DECLARE_LIBID(LIBID_MirandaSearchLib)
	DECLARE_REGISTRY_APPID_RESOURCEID(IDR_MIRANDASEARCH, "{478232E9-A7BD-4FD6-AAB4-D6A54651AAF9}")

	BOOL DllMain(DWORD dwReason, LPVOID lpReserved);
	HRESULT DllRegisterServer(BOOL bRegTypeLib = TRUE);
	HRESULT DllUnregisterServer(BOOL bUnRegTypeLib = TRUE);

	// Open command pipe
	BOOL Open();

	// Close command pipe
	void Close();

	// Retrieve data from Miranda IM using command pipe
	BOOL ReadData(LPCSTR szURL, CAtlArray< BYTE >& pAnswer);

	inline BOOL ReadData(LPCWSTR szURL, CAtlArray< BYTE >& pAnswer) { return ReadData( UTF8Encode( szURL ), pAnswer ); }

	// Register client pipe in the list to remember to disconnect still active clients on exit
	void RegisterClientPipe(HANDLE hPipe);

	// Disconnect, close and remove client pipe from the list
	void CloseClientPipe(HANDLE hPipe);

	// Disconnect active client pipes
	void KickClientPipes();

	// Full pathname of current plugin
	CString					m_sModule;

	// Plugin version
	BYTE					m_nVersion[ 4 ];

	// Windows version
	OSVERSIONINFO			m_OSVersion;

	const CString& GetMe() const { return m_sMe; }

	const CStringA GetProductName() const { return m_sProductName; }

protected:
	// Command pipe to Miranda IM
	HANDLE					m_hCommandPipe;

	// Active client pipes on Miranda IM side
	CAtlList< HANDLE >		m_hClientPipes;

	// My nick from Miranda IM
	CString					m_sMe;

	// Command pipe synchronization
	CComAutoCriticalSection	m_pSection;

	// Data loaded from version info resource
	CStringA				m_sFileDescription, m_sLegalCopyright, m_sProductName, m_sComments, m_sCompanyName, m_sEMail;
};

extern class CMirandaSearchModule _AtlModule;

// Get "miranda://{current user SID}/" URL
CString GetCurrentUserURL();

// Parse "miranda://" URL
URLLevel ParseURL(const CString& sURL, CString& sRelativeURL);

// Create (or delete) search URL as Windows Search root and scope
HRESULT ManageURL(BOOL bCreate, BOOL bClean, LPCWSTR szURL);

// Notify Windows Search about added/changed URL
// nKind - SEARCH_CHANGE_ADD, SEARCH_CHANGE_DELETE or SEARCH_CHANGE_MODIFY
HRESULT OnURLChanged(LPCWSTR szURL, SEARCH_KIND_OF_CHANGE nKind, BOOL bHighPriority = FALSE);

// Is a 32-bit application running under 64-bit Windows?
BOOL Is64Windows();
